package com.open.capacity.trade.service.impl;

import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Concurrent;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.dto.EditSalaryDto;
import com.open.capacity.trade.dao.ConcurrentDao;
import com.open.capacity.trade.feign.TradeFeignClient;
import com.open.capacity.trade.model.ConcurrentExcel;
import com.open.capacity.trade.service.ConcurrentService;
import com.open.capacity.trade.vo.ConcurrentVo;
import com.open.capacity.trade.vo.SalaryInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class ConcurrentServiceImpl implements ConcurrentService {
    @Autowired
    private ConcurrentDao concurrentDao;
    @Autowired
    private TradeFeignClient tradeFeignClient;
    @Override
    public PageResult<ConcurrentVo> findAllConcurrent(Map<String, Object> params) throws ServiceException {
        try {
            //由于没有初始化数据，就先在页面展示数据时进行数据的导入
            //调用其它模块的feignClient实现远程调用
            //再将得到的数据进行导入表中
            //之后数据就是动态插入
//            Concurrent concurrent = new Concurrent();
//            this.insertConcurrent(concurrent);
            int total = concurrentDao.count(params);
            List<ConcurrentVo> list = Collections.emptyList();
            if (total > 0) {
                PageUtil.pageParamConver(params, true);
                list = concurrentDao.findAllConcurrent(params);
            }
            return PageResult.<ConcurrentVo>builder().data(list).code(0).count((long) total).build();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Transactional
    @Override
    public void deleteConcurrent(Long id,Long concurrentId) throws ServiceException {
        try{
            Concurrent concurrent=concurrentDao.findById(id,concurrentId);
            concurrentDao.deleteConcurrent(id,concurrentId);
            log.info("删除兼职工资记录：{}",concurrent);
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public Result saveOrUpdate(Concurrent concurrent) throws ServiceException {
        try {
            concurrent.setUpdateTime(new Date());
            int result=0;
            result=concurrentDao.updateOrEnabled(concurrent);
            return result > 0 ? Result.succeed( "操作成功") : Result.failed("操作失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public List<ConcurrentExcel> findAllConcurrentExcel(Map<String, Object> params) throws ServiceException {
        try {
            List<ConcurrentExcel> concurrentExcels = new ArrayList<>();
            List<ConcurrentVo> list = concurrentDao.findAllConcurrent(params);

            for (ConcurrentVo concurrent : list){
                ConcurrentExcel concurrentExcel = new ConcurrentExcel();
                BeanUtils.copyProperties(concurrent,concurrentExcel);
                concurrentExcels.add(concurrentExcel);
            }
            return concurrentExcels;
        } catch (BeansException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Result insertConcurrent(Concurrent concurrent) throws ServiceException {
        try{
//            PageResult<Object> objectPageResult = tradeFeignClient.selectAllConcurrentDate();//先查询出所有数据
            String getTime;
            synchronized (concurrent){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
                getTime=simpleDateFormat.format(new Date());
            }
            String r=getTime.replace(" ","");
            String c=r.replaceAll(":","");
            Long time=Long.valueOf(String.valueOf(c));
//            Concurrent concurrent = new Concurrent();
            concurrent.setConcurrentId(time);
//            concurrentDao.insertConcurrent(concurrent);//把数据插入数据库
            concurrent.setCreateTime(new Date());
            int result=0;
            result=concurrentDao.insertConcurrent(concurrent);
            return result > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public Result updateConcurrent(Concurrent concurrent) throws ServiceException {
        try{
            concurrent.setUpdateTime(new Date());
            Integer result=concurrentDao.updateConcurrent(concurrent);
            return result > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public SalaryInfoVo findSalaryByBatchId(Long id) {
        return concurrentDao.findSalaryByBatchId(id);
    }

    @Override
    public boolean editSalaryByBatchId(EditSalaryDto dto) {
        return concurrentDao.editSalaryByBatchId(dto)>0;
    }
}
